package com.coin900.coin.cmd.coin_project;

import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.persist.coin.CoinProjectCoinVO;
import com.coin900.coin.persist.coin.CoinProjectVO;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.service.coin.ICoinProjectService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 新增项目
 *
 * @author shui
 * @create 2018-01-15
 **/
@Controller
public class CoinProjectPostCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        CoinProjectVO coinProjectVO = new Gson().fromJson(source, CoinProjectVO.class);
        coinProjectVO.setCreatedInfor(user);
        // 项目支持的虚拟币
        List<CoinProjectCoinVO> projectCoinList = coinProjectVO.getProjectCoinList();
        if (projectCoinList != null) {
            for (CoinProjectCoinVO coinProjectCoinVO : projectCoinList) {
                coinProjectCoinVO.setCreatedInfor(user);
            }
        }
        this.projectService.insertCoinProject(coinProjectVO);
        return respSign;
    }

    @Autowired
    private ICoinProjectService projectService;

    @Override
    public String getCmdCode() {
        return CmdCode.COIN_PROJECT_POST;
    }
}
